angular.module('feibo.teacher.controller')
    .controller('teacherContractCtrl', ['$scope', '$stateParams', 'TeacherService', 'kvMap', 'CommonService',
            function($scope, $stateParams, TeacherService, kvMap, CommonService){
                $scope.contract = {
                    list: undefined,
                    bigClassType: {},
                    search: $stateParams.schoolId,
                    keyword: {
                        signed_date: false,
                        contract_no: false,
                        school: false,
                        effect: false
                    }
                };


                $scope.initData = function(){
                    $scope.getTeacherContractList();
                    $scope.fetchClassType();
                };

                $scope.sorting = function(keyword){
                    var data = $scope.contract;

                    data.list.sort(function(a, b){
                        var x, y;
                        if(a && b && typeof a === 'object' && typeof b === 'object'){

                            if(data.keyword[keyword]){
                                x = a[keyword];
                                y = b[keyword];
                            }else {
                                x = b[keyword];
                                y = a[keyword];
                            }

                            if(x == y){
                                return 0;
                            }else if(typeof x === typeof y){
                                return  x >  y ? 1 : -1;
                            }else {
                                return typeof x > typeof y ? 1 : -1;
                            }
                        }
                    });

                    data.keyword[keyword] = !data.keyword[keyword];
                };

                $scope.getTeacherContractList = function(){
                    var queryConfig = {
                        funcName: 'getTeacherContractList',
                        container: $scope.contract,
                        propName: 'list',
                        propChain: ['data'],
                        showLoadLayer: false,
                        httpGetParams: {
                            search: $scope.contract.search || undefined
                        }
                    };

                    TeacherService.getTeacherContractList(queryConfig).catch(function(error){
                        console.error(error);
                    });
                };

                $scope.fetchClassType = function(isFirst){
                    var queryConfig = {
                        funcName: 'fetchClassType',
                        propChain: [],
                        showLoadLayer: false
                    };

                    TeacherService.fetchClassType(queryConfig).then(function(returnedData){
                        if(returnedData.success && returnedData.data.entries){
                            returnedData.data.entries.forEach(function(val){
                                $scope.contract.bigClassType[val.id] = val.name;
                            });
                        }
                    }).catch(function(error){
                        console.error(error);
                    });
                };

        }]);
